<?php
namespace app\cron\controller;
use app\common\controller\BaseApi;

class Index extends BaseApi
{
    public function _initialize()
    {
        $sk= input("sk/s");
        if ($sk != "27615e64890e433c74e9eb34f974d07e") {
            header('Content-Type:text/html; charset=utf-8');
        	echo "404";die;
        }
    }

    public function index()
    {
//        $month_later = date("Y-m-d", strtotime("1 month"));
//        $today = date("Y-m-d");
//        $res = db("GymOrder")->where(" member_ship_id=71 and shop_id=71 and is_delete=0 and end_date <= '$month_later' and end_date >= '$today'")->fetchSql(true)->count(1);
//        var_dump($res);
        exit();
    }

    /**
     * 统计店铺总记录
     */
    public function statisticShopExtra()
    {
        //每个店铺会员统计
        $list =	db("Interface")->where(array("is_original" => 1, "shop_id" => array("gt", 0)))->group("shop_id")->column("shop_id,sum(owner_type=1) as member_count,sum(owner_type=2) as employee_count");
        //每个店铺刷卡统计
        $logs = db("BehaviorLog")->group("shop_id")->column("shop_id,sum(owner_type=1) as member_enter_count,sum(owner_type=2) as employee_enter_count,sum(owner_type=3) as temporary_enter_count");
        //每个店铺登录统计
        $logins = db("AdminLog")->group("shop_id")->column("shop_id,count(1)");
        //店铺业绩
        $follow_order_price = db("GymOrderHistory")->group("shop_id")->column("shop_id, sum(pay_money) as follow_price");
        $pt_order_price = db("GymPtOrderHistory")->group("shop_id")->column("shop_id, sum(price) as pt_price");

        foreach ($list as $key => $value) {
            if(isset($logs[$key])){
                $list[$key]['member_enter_count'] = $logs[$key]['member_enter_count'];
                $list[$key]['employee_enter_count'] = $logs[$key]['employee_enter_count'];
                $list[$key]['temporary_enter_count'] = $logs[$key]['temporary_enter_count'];
            }else{
                $list[$key]['member_enter_count'] = 0;
                $list[$key]['employee_enter_count'] = 0;
                $list[$key]['temporary_enter_count'] = 0;
            }
            if(isset($logins[$key])){
                $list[$key]['login_count'] = $logins[$key];
            }else{
                $list[$key]['login_count']=0;
            }

            if(isset($follow_order_price[$key]) ) {
                $list[$key]['achievement_count'] = $follow_order_price[$key];
            } else{
                $list[$key]['achievement_count'] = 0;
            }

            if(isset($pt_order_price[$key]) ) {
                $list[$key]['achievement_count'] += $pt_order_price[$key];
            } else{
                $list[$key]['achievement_count'] += 0;
            }
        }
        //先删除 再添加
        db("shop_extra")->delete(true);
        db("shop_extra")->insertAll($list);
        header('Content-Type:text/html; charset=utf-8');
        echo "statisticShopExtra--success";die;
    }

    /**
     * 统计每日
     */
    public function statisticShopEveryday()
    {
        $yestoday = date("Y-m-d", time() - 86400);
		$count=db("shop_statistic")->where("date='".date('Ymd',strtotime("-1 day"))."'")->count(1);
		if($count>0){
			header('Content-Type:text/html; charset=utf-8');
       		 echo "statisticShopEveryday--once agin";die;
		}
        //每个店铺会员统计
        $list=	db("Interface")->where(array("is_original" => 1, "shop_id" => array("gt", 0)))->group("shop_id")->column("shop_id, count(1) as card_count, owner_type");
        //每个店铺刷卡统计
        $logs = db("BehaviorLog")->where("date_format(create_time,'%Y-%m-%d')= '$yestoday' ")->group("shop_id")->column("shop_id,sum(owner_type=1) as member_enter_count,sum(owner_type=2) as employee_enter_count,sum(owner_type=3) as temporary_enter_count");
        //每个店铺登录统计
        $logins=db("AdminLog")->where("date_format(create_time,'%Y-%m-%d')= '$yestoday' ")->group("shop_id")->column("shop_id,count(1)");
		//店铺业绩
        $follow_order_price = db("GymOrderHistory")->where("date_format(create_time,'%Y-%m-%d')='$yestoday' ")->group("shop_id")->column("shop_id, sum(pay_money) as follow_price");
        $pt_order_price = db("GymPtOrderHistory")->where("date_format(create_time,'%Y-%m-%d')='$yestoday' ")->group("shop_id")->column("shop_id, sum(price) as pt_price");
        foreach ($list as $key => $value) {
            if(isset($logs[$key])){
                $list[$key]['member_enter_count'] = $logs[$key]['member_enter_count'];
                $list[$key]['employee_enter_count'] = $logs[$key]['employee_enter_count'];
                $list[$key]['temporary_enter_count'] = $logs[$key]['temporary_enter_count'];
            }else{
                $list[$key]['member_enter_count'] = 0;
                $list[$key]['employee_enter_count'] = 0;
                $list[$key]['temporary_enter_count'] = 0;
            }
            if(isset($logins[$key])){
                $list[$key]['login_count'] = $logins[$key];
            }else{
                $list[$key]['login_count'] = 0;
            }
            if(isset($follow_order_price[$key]) ) {
                $list[$key]['achievement_count'] = $follow_order_price[$key];
            } else{
                $list[$key]['achievement_count'] = 0;
            }

            if(isset($pt_order_price[$key]) ) {
                $list[$key]['achievement_count'] += $pt_order_price[$key];
            } else{
                $list[$key]['achievement_count'] += 0;
            }
            $list[$key]['date'] = date("Ymd", time() - 86400);
            unset($list[$key]['owner_type']);
        }
        db("shop_statistic")->insertAll($list);
      	header('Content-Type:text/html; charset=utf-8');
        echo "statisticShopEveryday--success";die;
    }
}
